import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_scan_tool/models/app_theme.dart';
import 'package:flutter_scan_tool/page/splash_page.dart';
import 'package:flutter_scan_tool/utils/SessionUtils.dart';

import 'bloc/device/device_bloc.dart';
import 'bloc/simple_bloc_observer.dart';
import 'bloc/theme/theme_bloc.dart';
import 'bloc/tool/tool_bloc.dart';
import 'bloc/tool/tool_event.dart';

void main() async {
  Bloc.observer = SimpleBlocObserver();
  WidgetsFlutterBinding.ensureInitialized();
  // NotificationPlugin.ensureInitialized();
  await SessionUtils.sharedInstance().init();
  await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]).then((_) => runApp(MyApp()));
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<ThemeBloc>(
      create: (context) => ThemeBloc()..add(ThemeLoadEvent()),
      child: BlocProvider<DeviceBloc>(
        create: (context) => DeviceBloc()..add(DeviceConnectEvent()),
        child: BlocProvider<ToolBloc>(
          create: (context) => ToolBloc()..add(ToolLoadEvent({})),
          child: BlocBuilder<ThemeBloc, ThemeState>(
            builder: (context, themeState) {
              return MaterialApp(
                title: 'Tool Management',
                debugShowCheckedModeBanner: false,
                theme: AppTheme.appTheme
                    .themeData()
                    .copyWith(platform: TargetPlatform.iOS),
                home: SplashPage(),
              );
            },
          ),
        ),
      ),
    );
  }
}
